package com.snow.useviewdemo.util.rlv.decoration;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.snow.useviewdemo.util.UAppUtils;

import org.jetbrains.annotations.NotNull;

/**
 * Created by zhyt
 * Date: 2022/4/8
 * Describe:
 */
public class LinearItemDecoration extends RecyclerView.ItemDecoration {
    private int spacing;//行间距
    private int firstSpacing;
    private int lastSpacing;
    //LinearLayoutManager.HORIZONTAL
    private int orientation;
    private Drawable mDivider;
    private final Rect mBounds = new Rect();

    public LinearItemDecoration(int spacing, int firstSpacing, int lastSpacing) {
        this(spacing, firstSpacing, lastSpacing, LinearLayoutManager.VERTICAL);
    }

    public LinearItemDecoration(int firstSpacing, int spacing, int lastSpacing, int orientation) {
        this.firstSpacing = firstSpacing;
        this.spacing = spacing;
        this.lastSpacing = lastSpacing;
        this.orientation = orientation;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view); // 获取view 在adapter中的位置。
        int totalCount = 0;
        if (parent.getAdapter() != null) {
            totalCount = parent.getAdapter().getItemCount();
        }
        if (orientation == LinearLayoutManager.HORIZONTAL) {
            if (position == 0) {//第一个
                if (UAppUtils.init().isRtl()) {
                    outRect.right = firstSpacing;
                } else {
                    outRect.left = firstSpacing;
                }
            } else if (position == totalCount - 1) {//最后一个
                if (UAppUtils.init().isRtl()) {
                    outRect.right = spacing;
                    outRect.left = lastSpacing;
                } else {
                    outRect.left = spacing;
                    outRect.right = lastSpacing;
                }
            } else {//中间其它的
                if (UAppUtils.init().isRtl()) {
                    outRect.right = spacing;
                } else {
                    outRect.left = spacing;
                }
            }
        } else {
            if (position == 0) {//第一个
                outRect.top = firstSpacing;
            } else if (position == totalCount - 1) {//最后一个
                outRect.top = spacing;
                outRect.bottom = lastSpacing;
            } else {//中间其它的
                outRect.top = spacing;
            }
        }
    }

    public void setDrawable(@NonNull Drawable drawable) {
        if (drawable == null) {
            throw new IllegalArgumentException("Drawable cannot be null.");
        } else {
            this.mDivider = drawable;
        }
    }

    @Override
    public void onDraw(@NonNull @NotNull Canvas c, @NonNull @NotNull RecyclerView parent, @NonNull @NotNull RecyclerView.State state) {
        if (parent.getLayoutManager() != null && this.mDivider != null) {
            if (this.orientation == LinearLayoutManager.VERTICAL) {
                this.drawVertical(c, parent);
            } else {
                this.drawHorizontal(c, parent);
            }

        }
    }

    private void drawVertical(Canvas canvas, RecyclerView parent) {
        canvas.save();
        int left;
        int right;
        if (parent.getClipToPadding()) {
            left = parent.getPaddingLeft();
            right = parent.getWidth() - parent.getPaddingRight();
            canvas.clipRect(left, parent.getPaddingTop(), right, parent.getHeight() - parent.getPaddingBottom());
        } else {
            left = 0;
            right = parent.getWidth();
        }

        int childCount = parent.getChildCount();

        //当最后一个子项的时候去除下划线
        for (int i = 0; i < childCount - 1; ++i) {
            View child = parent.getChildAt(i);
            parent.getDecoratedBoundsWithMargins(child, this.mBounds);
            int bottom = this.mBounds.bottom + Math.round(child.getTranslationY());
            int top = bottom - this.mDivider.getIntrinsicHeight();
            this.mDivider.setBounds(left, top, right, bottom);
            this.mDivider.draw(canvas);
        }
        canvas.restore();
    }

    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
        canvas.save();
        int top;
        int bottom;
        if (parent.getClipToPadding()) {
            top = parent.getPaddingTop();
            bottom = parent.getHeight() - parent.getPaddingBottom();
            canvas.clipRect(parent.getPaddingLeft(), top, parent.getWidth() - parent.getPaddingRight(), bottom);
        } else {
            top = 0;
            bottom = parent.getHeight();
        }

        int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; ++i) {
            View child = parent.getChildAt(i);
            parent.getLayoutManager().getDecoratedBoundsWithMargins(child, this.mBounds);
            int right = this.mBounds.right + Math.round(child.getTranslationX());
            int left = right - this.mDivider.getIntrinsicWidth();
            this.mDivider.setBounds(left, top, right, bottom);
            this.mDivider.draw(canvas);
        }
        canvas.restore();
    }
}
